package com.lxk.service.impl;

import com.lxk.dao.*;
import com.lxk.pojo.*;
import com.lxk.service.RefundService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.PageModel;
import com.lxk.vo.RefundDetailModel;
import com.lxk.vo.RefundModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/3/31 16:10
 * @description
 */
@Service
@Transactional
public class RefundServiceImpl implements RefundService {

    @Autowired
    private OrdersMapper om;
    @Autowired
    private RefundMapper rm;
    @Autowired
    private CouponMapper cm;
    @Autowired
    private GoodsMapper gm;
    @Autowired
    private WxUserMapper wum;
    @Autowired
    private AsMyMapper mm;

    public Integer generateRefundOrder(Integer ordersId) {
        Orders orders = om.selectByPrimaryKey(ordersId);
        RefundExample example = new RefundExample();
        RefundExample.Criteria criteria = example.createCriteria();
        criteria.andOrdersIdEqualTo(ordersId);
        List<Refund> list = rm.selectByExample(example);
        if (list.size()==0) {
            Refund refund = new Refund();
            refund.setOrdersId(ordersId);
            refund.setHandleTime(new Date());
            refund.setRefundTime(new Date());
            refund.setOutTradeNo(orders.getOutTradeNo());
            refund.setRefundPrice(orders.getOrdersPrice());
            refund.setRefundReason("商家发起退款");
            refund.setRefundStatus(1);
            refund.setWxUserId(orders.getWxUserId());
            int ins = rm.insertSelective(refund);
            return ins;
        }else{
            Refund refund = list.get(0);
            refund.setRefundStatus(1);
            refund.setHandleTime(new Date());
            int upd = rm.updateByPrimaryKeySelective(refund);
            return upd;
        }
    }

    public Integer changeRefundStatus(Integer ordersId, Integer refundStatus) {
        RefundExample example = new RefundExample();
        RefundExample.Criteria criteria = example.createCriteria();
        criteria.andOrdersIdEqualTo(ordersId);
        List<Refund> list = rm.selectByExample(example);
        if (list.size()==0) {
            return 0;
        }else{
            Refund refund = list.get(0);
            refund.setRefundStatus(refundStatus);
            int upd = rm.updateByPrimaryKeySelective(refund);
            return upd;
        }
    }

    public Integer rollbackCoupon(Integer ordersId) {
        CouponExample ce = new CouponExample();
        CouponExample.Criteria criteria = ce.createCriteria();
        criteria.andOrdersIdEqualTo(ordersId);
        List<Coupon> list = cm.selectByExample(ce);
        if(list.size()>0) {
            Coupon coupon = list.get(0);
            coupon.setIsUsed(0);
            coupon.setOrdersId(null);
            int upd = cm.updateByPrimaryKeySelective(coupon);
            return upd;
        }else {
            return 2;
        }
    }

    public Integer getTotalPages(Integer num, Integer refundStatus) {
        Integer count = mm.countRefunds(refundStatus);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<RefundModel> selRefundPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order, Integer refundStatus) {
        int start = (pageIndex-1)*num;
        List<RefundModel> list = mm.selRefundsPage(start, num, sort, order, refundStatus);
        PageModel<RefundModel> model = new PageModel<RefundModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer rejectRefund(Integer refundId) {
        Refund refund = rm.selectByPrimaryKey(refundId);
        refund.setRefundStatus(3);
        refund.setHandleTime(new Date());
        rm.updateByPrimaryKeySelective(refund);
        Integer ordersId = refund.getOrdersId();
        Orders orders = om.selectByPrimaryKey(ordersId);
        orders.setOrdersStatus(9);
        om.updateByPrimaryKeySelective(orders);
        return 1;
    }

    public Refund selRefundById(Integer refundId) {
        Refund refund = rm.selectByPrimaryKey(refundId);
        return refund;
    }

    public RefundDetailModel getRefundDetail(Integer refundId) {
        Refund refund = rm.selectByPrimaryKey(refundId);
        if (refund==null) {
            throw new GlobalException(CodeMsg.NO_REFUND_ORDER);
        }
        Integer ordersId = refund.getOrdersId();
        Orders orders = om.selectByPrimaryKey(ordersId);
        Integer goodsId = orders.getGoodsId();
        Integer wxUserId = orders.getWxUserId();
        Goods goods = gm.selectByPrimaryKey(goodsId);
        WxUser wxUser = wum.selectByPrimaryKey(wxUserId);
        RefundDetailModel model = new RefundDetailModel();
        model.setGoods(goods);
        model.setOrders(orders);
        model.setRefund(refund);
        model.setWxUser(wxUser);
        return model;
    }

}
